<template>
	<div>
		<van-nav-bar
		  title="登录"
		/>
		<van-cell-group>
		  <van-field 
				v-model="addForm.username" 
				label="账号" 
				placeholder="请输入用户名" 
				clearable
			/>
			<van-field 
				v-model="addForm.password" 
				type="password" 
				label="密码" 
				placeholder="请输入密码" 
				clearable
			/>
		</van-cell-group>
		<div style="text-align: center;padding: 15px;">
			<van-button type="primary" @click="handleLogin">登录</van-button>
			<van-button type="warning" @click="handleRegister">注册</van-button>
		</div>
		
	</div>
</template>

<script>
	import {login} from '@/api/user-api.js'
	export default{
		data(){
			return {
				addForm:{
					username:'',
					password:''
				}
			}
		},
		methods:{
			async handleLogin(){
				let res = await login(this.addForm.username,this.addForm.password)
				console.log(res)
				if(res.data.code == 200 ){
					
					sessionStorage.userInfo = JSON.stringify(res.data.data.userInfo);
					sessionStorage.token = res.data.data.jwt.access_token;
					
					this.$router.push('/')
				}
			},
			handleRegister(){
				this.$router.push('/register')
			}
		}
	}
</script>

<style>
</style>
